﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Device
{
	public partial class DeviceMaster : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            Linker();
            AudienceBind();
            AccessBind();
		}


        //是否可以链接
        void Linker()
        {
            DateTime time = DateTime.Now;
            Util.Result result = Util.OverTime(time);
            switch (result)
            {
                case Util.Result.Success:
                    break;
                case Util.Result.OverTime:
                    Response.Redirect("OverTime.aspx");
                    break;
                case Util.Result.Login:
                    break;
            }
        }


        //显示设备主人下所有的观众表
        void AudienceBind()
        {
            using (Model1Container db = new Model1Container())
            {
                Audience sh = new Audience();            
                var id = Convert.ToInt32(Util.UserId());
                List<Audience > user=(from u in db .Audience集 where u.ownerid ==id select u).ToList ();
                GridView1.DataSource = user;
                GridView1.DataBind();
            }
        }
      
        //显示控制表中的信息
        void AccessBind()
        {
            using (Model1Container db = new Model1Container())
            {
                GridView4.DataSource = db.Accesstable集;
                GridView4.DataBind();
            }
        }

        //显示设备主人下所有的观众表
        void AudienceBind3()
        {
            using (Model1Container db = new Model1Container())
            {
                Audience sh = new Audience();
                var id = Convert.ToInt32(Util.UserId());
                List<Audience> user = (from u in db.Audience集 where u.ownerid == id select u).ToList();
                GridView3.DataSource = user;
                GridView3.DataBind();
            }
        }

        //添加观众用户
        void AddUser(string email, string password, Int32 ownerid)
        {
            using (Model1Container db = new Model1Container())
            {
                var password_sha = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                Audience sh = new Audience();
                sh.ownerid = ownerid;
                sh.email = email;
                sh.password = password_sha;
                sh.loginpower = "禁用";
                db.Audience集.AddObject(sh);
                db.SaveChanges();
            }
        }

        

       //删除用户
        void delete()
        {
            using (Model1Container db = new Model1Container())
            {             
                Audience sh = new Audience();
                sh.id = Convert.ToInt32(TextBox7.Text.Trim());
                db.Audience集.Attach(sh);
                db.ObjectStateManager.ChangeObjectState(sh, System.Data.EntityState.Deleted);
                db.SaveChanges();
                AudienceBind();
            }
        }

        //添加用户
        protected void Button11_Click(object sender, EventArgs e)
        {
            var str = "抱歉!该邮箱以被注册!";
            Int32 ownerid = Convert.ToInt32(Util.UserId());
            var email = TextBox1.Text.Trim();
            var password = TextBox6.Text.Trim();
            Audiences.AuthResult result = Audiences.Email(email );
            switch (result)
            {
                case Audiences.AuthResult.ExistEmail:
                    Util.AlertString(str, Response);
                    break;
                case Audiences.AuthResult.Add:
                    AddUser(email, password, ownerid);
                    AudienceBind();
                    break;
            }
        }

        //删除用户
        protected void Button10_Click(object sender, EventArgs e)
        {
            string Del1 = "请输想要删除的用户名!";
            //string Del2 = "确定要删除该用户吗?";
            string Del3 = "抱歉!该用户不存在!";
            string Del4 = "删除成功!";
            var ownerid = Convert.ToInt32(Util.UserId());
            if (TextBox7.Text == "")
            {
                Util.AlertString(Del1, Response);
            }
            else
            {
                var id = Convert.ToInt32(TextBox7.Text);
                Audiences.AuthResult result = Audiences.UserId(id,ownerid);
                switch (result)
                {
                    case Audiences.AuthResult.InExistid:
                        Util.AlertString(Del3, Response);
                        break;
                    case Audiences.AuthResult.Del:
                        //怎样可以确认一下
                       
                        delete();
                        Util.AlertString(Del4, Response);
                        AudienceBind();
                        break;
                }
            }
        }

        //修改用户登陆权限
        protected void Button12_Click(object sender, EventArgs e)
        {
            string str="请填写所要修改用户的id";
            string str1 = "抱歉!该用户不存在!";
            string str2="修改成功!";
            var ownerid = Convert.ToInt32(Util.UserId());
            if (TextBox5.Text.Trim() == "")
            {
                Util.AlertString(str, Response);
            }
            else
            {
                 var id = Convert.ToInt32(TextBox5.Text);
                Audiences.AuthResult result = Audiences.UserId(id,ownerid);
                switch (result)
                {
                    case Audiences.AuthResult.InExistid:
                        Util.AlertString(str1, Response);
                        break;
                    case Audiences.AuthResult.Del:
                       control();
                        Util.AlertString(str2, Response);
                        AudienceBind3();
                        break;
                }
            }
        }
       

                //修改用户权限
        void control()
        {

            Int32 id = Convert.ToInt32(TextBox5.Text.Trim());
            Model1Container db = new Model1Container();
            Audience sh = db.Audience集.FirstOrDefault(cc => cc.id == id);
            sh.loginpower = DropDownList2.Text.Trim();
            Audience update = db.Audience集.FirstOrDefault(cc => cc.id == id);
            db.SaveChanges();
            AudienceBind();
        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            string Str1 = "请填写用户名和密码!";
            string Str2 = "该用户不存在";
            if (TextBox1.Text.Trim() == "" && TextBox6.Text.Trim() == "")
            {
                Util.AlertString(Str1, Response);
            }
            else
            {
                var email = TextBox1.Text.Trim();
                Int32 ownerid = Convert.ToInt32(Util.UserId());
                Audiences.AuthResult result = Audiences.Email(email );
                switch (result)
                {
                    case Audiences.AuthResult.ExistEmail:
                        altermima();
                        AudienceBind();
                        break;
                    case Audiences.AuthResult.Add:
                        Util.AlertString(Str2, Response);                      
                        break;
                    case Audiences.AuthResult.Ownerid :
                        Util.AlertString(Str2, Response);
                        break;
                }
            }
        }

        //修改用户密码
        void altermima()
        {
            var Str2 = "抱歉!该用户不存在!";
            try
            {
                var id = Convert.ToInt32(Util.UserId());
                Model1Container db = new Model1Container();
                Audience psw = db.Audience集.FirstOrDefault(cc => cc.email == TextBox1.Text.Trim() && cc.ownerid == id);
                psw.password = TextBox6.Text.Trim();
                Audience update = db.Audience集.FirstOrDefault(cc => cc.email == TextBox1.Text.Trim());
                db.SaveChanges();
            }
            catch (Exception em)
            {
                Util.AlertString(Str2, Response);
            }
        }

        protected void Button15_Click(object sender, EventArgs e)
        {
            string str = "请填写所要修改的设备id";
            string str1 = "抱歉!该设备不存在!";
            string str2 = "修改成功!";
            Int32 ownerid = Convert.ToInt32(Util.UserId());
            if (TextBox8.Text.Trim() == "")
            {
                Util.AlertString(str, Response);
            }
            else
            {
                var id = Convert.ToInt32(TextBox8.Text);
                Audiences.AuthResult result = Audiences.DeviceId(id,ownerid);
                switch (result)
                {
                    case Audiences.AuthResult.InExistDevid :
                        Util.AlertString(str1, Response);
                        break;
                    case Audiences.AuthResult.Existid :
                        AlterDevicePower();
                        Util.AlertString(str2, Response);                      
                        break;
                }
            }
        }

        //修改设备权限
        void AlterDevicePower()
        {
            Int32 id = Convert.ToInt32(TextBox8.Text.Trim());
            Model1Container db = new Model1Container();
            Accesstable sh = db.Accesstable集.FirstOrDefault(cc => cc.equimentid  == id);
            sh .lookpower  = DropDownList3.Text.Trim();
            Audience update = db.Audience集.FirstOrDefault(cc => cc.id == id);
            db.SaveChanges();
            AccessBind();
        }

	}
}